﻿Public Class FrmActCosto

    Private grupo As GBDZafiro.Grupo
    Dim subGr As GBDZafiro.SubGrupo
    Private i As Integer = 0
    Private gv As DataGridView

    Private Sub FrmActCosto_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        tbFecha.Text = Date.Now.ToLongDateString.ToString
    End Sub

    Private Sub btBuscar_Click(sender As System.Object, e As System.EventArgs) Handles btBuscar.Click
        Dim oFrmBGrupo As New FrmBcarGrupo
        Dim dtProd As DataTable
        Dim dlg As DialogResult = oFrmBGrupo.ShowDialog()
        If dlg = DialogResult.OK Then
            grupo = oFrmBGrupo.grupo
            tbGrupo.Text = grupo.grupo
            dtProd = GBDZafiro.Producto.consCostoUni(CInt(grupo.idGrupo))
            cargarGvProd(gvProd, dtProd)
            tbTotalProd.Text = dtProd.Rows.Count.ToString
            oFrmBGrupo.Close()
        End If
    End Sub

    Private Sub btBuscar2_Click(sender As System.Object, e As System.EventArgs) Handles btBuscar2.Click
        If Not tbSubGrupo.TextLength < 1 Then
            subGr = New GBDZafiro.SubGrupo
            subGr.Descripcion = tbSubGrupo.Text
            Dim dtProd As DataTable = GBDZafiro.Producto.consCostoUni(subGr.Descripcion)
            cargarGvProd(gvProd2, dtProd)
            tbTotalProd2.Text = dtProd.Rows.Count.ToString
            tbProd2.Text = i
        End If
    End Sub

    Private Sub gvProd_CurrentCellChanged(sender As System.Object, e As System.EventArgs)
     
    End Sub

    Private Sub gvProd_CellValueChanged(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs)
        RemoveHandler gv.CellValueChanged, AddressOf gvProd_CellValueChanged
        Dim chkCell As DataGridViewCheckBoxCell
        Dim rProd As DataGridViewRow = gv.CurrentRow
        rProd.Cells("NUEVO_COSTO").Value = CDec(rProd.Cells("NUEVO_COSTO").Value)

        If Not rProd.Cells("NUEVO_COSTO").Value = rProd.Cells("COSTO_UNIT").Value Then
            chkCell = rProd.Cells("chk")
            chkCell.Value = True
            rProd.DefaultCellStyle.BackColor = Color.GreenYellow

            i += 1

        Else
            chkCell = rProd.Cells("chk")
            chkCell.Value = False
            rProd.DefaultCellStyle.BackColor = Color.White

            i -= 1

        End If

        If gv.Name = gvProd.Name Then
            tbProd.Text = i

        ElseIf gvProd2.Name = gv.Name Then
            tbProd2.Text = i

        End If

        AddHandler gv.CellValueChanged, AddressOf gvProd_CellValueChanged
    End Sub

    Private Sub gvProd_EditingControlShowing(sender As System.Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles gvProd.EditingControlShowing
        Dim txtEdit As TextBox = e.Control
        AddHandler txtEdit.KeyPress, AddressOf txtEdit_Keypress
    End Sub

    Private Sub gvProd3_EditingControlShowing(sender As System.Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles gvProd3.EditingControlShowing
        Dim txtEdit As TextBox = e.Control
        AddHandler txtEdit.KeyPress, AddressOf txtEdit_Keypress
    End Sub

    Private Sub gvProd2_EditingControlShowing(sender As System.Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles gvProd2.EditingControlShowing
        Dim txtEdit As TextBox = e.Control
        AddHandler txtEdit.KeyPress, AddressOf txtEdit_Keypress
    End Sub

    Private Sub txtEdit_Keypress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
         Validaciones.SoloNumeros(sender, e)
    End Sub

    Private Sub btAplicar_Click(sender As System.Object, e As System.EventArgs) Handles btAplicar.Click
        aplicar(gvProd)
        cargarGvProd(gvProd)
        i = 0
        tbProd.Text = i
    End Sub

    Private Sub btAplicar2_Click(sender As System.Object, e As System.EventArgs) Handles btAplicar2.Click
        aplicar(gvProd2)
        cargarGvProd(gvProd2)
        i = 0
        tbProd2.Text = i
    End Sub

    Private Sub btAplicar3_Click(sender As System.Object, e As System.EventArgs) Handles btAplicar3.Click
        aplicar(gvProd3)
        cargarGvProd(gvProd3)
    End Sub

    Private Sub tcActCostos_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles tcActCostos.SelectedIndexChanged
        limpiarCampos(tcActCostos.SelectedIndex)
    End Sub

    Private Sub tbSubGrupo_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles tbSubGrupo.KeyDown
        If Validaciones.Enter(e) Then
            Dim ev As New System.EventArgs
            btBuscar2_Click(sender, ev)
            gvProd2.Focus()
        End If
    End Sub

    Private Sub btBuscar3_Click(sender As System.Object, e As System.EventArgs) Handles btBuscar3.Click
        Dim oFrmBP As New FrmBProducto
        oFrmBP.BuscarPor = "CODIGO"
        Dim dlg As DialogResult = oFrmBP.ShowDialog()
        If dlg = DialogResult.OK Then
            Dim oProd As New GBDZafiro.Producto
            oProd = oFrmBP.oProducto

            cargarGvProd(gvProd3, oProd)

            oFrmBP.Close()
        End If

    End Sub

    Private Sub btEliminar_Click(sender As System.Object, e As System.EventArgs) Handles btEliminar.Click
        Dim rProd As DataGridViewRow = gvProd3.CurrentRow
        gvProd3.Rows.Remove(rProd)
    End Sub

    Private Sub btAtras_Click(sender As System.Object, e As System.EventArgs) Handles btAtras.Click
        Dim rProd As DataGridViewRow = gvProd3.CurrentRow
        If rProd.Cells("chk").Value = True Then
            rProd.Cells("chk").Value = False
            rProd.Cells("NUEVO_COSTO").Value = rProd.Cells("COSTO_UNIT").Value
            rProd.DefaultCellStyle.BackColor = Color.White
        End If
    End Sub

    Private Sub limpiarCampos(i As Integer)
        Select Case i
            Case 0
                gvProd3.Rows.Clear()
            Case 1
                tbGrupo.Clear()
                gvProd.DataSource = Nothing
                gvProd.Columns.Clear()
                tbTotalProd.Clear()
                tbProd.Clear()
            Case 2
                tbSubGrupo.Clear()
                gvProd2.DataSource = Nothing
                gvProd2.Columns.Clear()
                tbTotalProd2.Clear()
                tbProd2.Clear()
        End Select

        Me.i = 0
    End Sub

    Private Function crearProd(rProd As DataGridViewRow) As GBDZafiro.Producto
        Dim prod As New GBDZafiro.Producto
        prod.codProducto = rProd.Cells("COD_PRODUCTO").Value
        prod.descripcion = rProd.Cells("DESCRIPCION").Value
        prod.idUniMed = rProd.Cells("ID_UNIDAD_MEDIDA").Value
        prod.costoUni = rProd.Cells("NUEVO_COSTO").Value
        Return prod
    End Function

    Private Sub cargarGvProd(ByRef gv As DataGridView, ByRef dt As DataTable)
        RemoveHandler gv.CellValueChanged, AddressOf gvProd_CellValueChanged

        Dim chk As New DataGridViewCheckBoxColumn
        Dim dr As New DataColumn
        chk.HeaderText = "D"
        chk.Name = "chk"

        dt.Columns.Add("NUEVO_COSTO", System.Type.GetType("System.Decimal"))
        For Each rProd As DataRow In dt.Rows
            rProd("NUEVO_COSTO") = rProd("COSTO_UNIT")
        Next

        With gv
            .Columns.Clear()
            .DataSource = Nothing
            .Rows.Clear()
            .DataSource = dt

            .Columns("COD_PRODUCTO").HeaderText = "CODIGO"
            .Columns("ID_UNIDAD_MEDIDA").HeaderText = "UNIDAD"
            .Columns("COSTO_UNIT").HeaderText = "COSTO"

            .Columns("COSTO_UNIT").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            .Columns("NUEVO_COSTO").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

            .Columns("COSTO_UNIT").DefaultCellStyle.Format = "N"
            .Columns("NUEVO_COSTO").DefaultCellStyle.Format = "N"

            .Columns.Add(chk)
            .Columns("chk").DisplayIndex = 0

            .Columns("COD_PRODUCTO").ReadOnly = True
            .Columns("DESCRIPCION").ReadOnly = True
            .Columns("ID_UNIDAD_MEDIDA").ReadOnly = True
            .Columns("COSTO_UNIT").ReadOnly = True
            .Columns("chk").ReadOnly = True
        End With
        AddHandler gv.CellValueChanged, AddressOf gvProd_CellValueChanged

        Me.gv = gv
        i = 0
        tbProd.Text = i
    End Sub

    Private Sub cargarGvProd(ByRef gv As DataGridView, ByRef Prod As GBDZafiro.Producto)
        RemoveHandler gv.CellValueChanged, AddressOf gvProd_CellValueChanged

        Dim rProd As DataGridViewRow = exisProd(gv, Prod)
        If rProd IsNot Nothing Then

        Else
            With Prod
                gvProd3.Rows.Add(False, _
                                 .codProducto, _
                                 .descripcion, _
                                 .idUniMed, _
                                 .costoUni, _
                                 .costoUni)
            End With

        End If

        AddHandler gv.CellValueChanged, AddressOf gvProd_CellValueChanged
        Me.gv = gv
    End Sub

    Private Sub cargarGvProd(ByRef gv As DataGridView)
        For Each rProd As DataGridViewRow In gv.Rows
            If rProd.Cells("chk").Value = True Then
                iniCosto(rProd)
            End If
        Next
    End Sub

    Private Function exisProd(ByRef gv As DataGridView, ByRef Prod As GBDZafiro.Producto) As DataGridViewRow
        For Each rProd As DataGridViewRow In gv.Rows
            If rProd.Cells("COD_PRODUCTO").Value = Prod.codProducto Then
                Return rProd
                Exit Function
            End If
        Next
        Return Nothing
    End Function

    Private Sub iniCosto(ByRef rProd)
        rProd.Cells("chk").Value = False
        rProd.Cells("COSTO_UNIT").Value = rProd.Cells("NUEVO_COSTO").Value
        rProd.DefaultCellStyle.BackColor = Color.White
    End Sub

    Private Sub aplicar(ByRef gv As DataGridView)
        For Each rProd As DataGridViewRow In gv.Rows
            If rProd.Cells("chk").Value = True Then
                Dim prod As GBDZafiro.Producto = crearProd(rProd)
                GBDZafiro.Producto.ActualizarCosto(prod)
            End If
        Next
    End Sub

End Class